home *** CD-ROM | disk | FTP | other *** search
- -- Problem 4.2
- -- by Rick Conn
- package Complex is
-
- type NUMBER is private;
-
- function Set (RP, IP : in FLOAT) return NUMBER;
- function "+" (Left, Right : in NUMBER) return NUMBER;
- function "-" (Left, Right : in NUMBER) return NUMBER;
- function "*" (Left, Right : in NUMBER) return NUMBER;
- procedure Print (Item : in NUMBER);
-
- private
-
- type NUMBER is record
- RP : FLOAT := 0.0; -- initialize to 0,0
- IP : FLOAT := 0.0;
- end record;
-
- end Complex;
-
- with Text_IO;
- package body Complex is
-
- package Float_IO is new Text_IO.Float_IO (FLOAT);
-
- function Set (RP, IP : in FLOAT) return NUMBER is
- begin
- return (RP, IP);
- end Set;
-
- function "+" (Left, Right : in NUMBER) return NUMBER is
- begin
- return (Left.RP + Right.RP, Left.IP + Right.IP);
- end "+";
-
- function "-" (Left, Right : in NUMBER) return NUMBER is
- begin
- return (Left.RP - Right.RP, Left.IP - Right.IP);
- end "-";
-
- function "*" (Left, Right : in NUMBER) return NUMBER is
- begin
- return (Left.RP * Left.RP - Right.RP * Right.RP,
- Left.RP * Right.IP + Left.IP * Right.RP);
- end "*";
-
- procedure Print (Item : in NUMBER) is
- begin
- Float_IO.Put (Item.RP, 5, 5, 0);
- Text_IO.Put (" + ");
- Float_IO.Put (Item.IP, 5, 5, 0);
- Text_IO.Put ("j");
- end Print;
-
- end Complex;
-
- with Text_IO;
- with Complex;
- use Complex; -- so infix operators may be used
- procedure Main is
-
- N1, N2, N3 : Complex.NUMBER;
-
- procedure Print_All is
- begin
- Text_IO.Put ("N1 = "); Complex.Print (N1); Text_IO.New_Line;
- Text_IO.Put (" N2 = "); Complex.Print (N2); Text_IO.New_Line;
- Text_IO.Put (" N3 = "); Complex.Print (N3); Text_IO.New_Line;
- end Print_All;
-
- begin -- Main
-
- Print_All;
- N1 := Set (2.2, 4.4);
- N2 := Set (1.0, 12.2);
- N3 := N1 + N2;
- Text_IO.Put_Line ("Sum:");
- Print_All;
- N3 := N1 - N2;
- Text_IO.Put_Line ("Difference:");
- Print_All;
- N3 := N1 * N2;
- Text_IO.Put_Line ("Product:");
- Print_All;
-
- end Main;
-